/************************
 * for Cloud animate
 */
var cloud_time = 2000;
function cloud_animate(id, key){
	$('#'+id).animate({
		marginLeft: parseInt($('#'+id).css('marginLeft'))+key*30
	},
	cloud_time,//animate time
	function(){
		cloud_animate(id, -key);
	});
}

//判断浏览器是否支持 placeholder
$(function(){
	//for cloud animate
	cloud_animate('cloud_s', -1);
	cloud_animate('cloud_m',  1);
	placeholderFn();
})
	function placeholderFn(){
		if(!placeholderSupport()){   
		    $('[placeholder]').focus(function() {
		        var input = $(this);
		        if (input.val() == input.attr('placeholder')) {
		            input.val('');
		            input.removeClass('placeholder');
		        }
		    }).blur(function() {
		        var input = $(this);
		        if (input.val() == '' || input.val() == input.attr('placeholder')) {
		            input.addClass('placeholder');
		            input.val(input.attr('placeholder'));
		        }
		    }).blur();
		}
		
		 //样式初始化
	    if ($('[placeholder]').value === "") {
	    	$('[placeholder]').value = $('[placeholder]').attr('placeholder'); 
	    }
	}


function placeholderSupport() {
    return 'placeholder' in document.createElement('input');
}


$(function(){
	
	$('#email,#password').focus(function(){
   		$('#beError').text('').hide();
   	}); 
	/**add nancy**/
	function immediately(){
		var element = document.getElementById("password");
		if("\v"=="v") {
			element.onpropertychange = webChange;
		}else{
			element.addEventListener("input",webChange,false);//此时的input为添加的oninput事件
		}
		function webChange(){
			$('#beError').text('').hide();
		}
	}
	immediately();
	/**end nancy**/
	//验证表单
	 	$("#loginForm").validate({
	 		/* onkeyup: false,
	 		focusCleanup:true,  */
	        rules: {
	    	   	email: {
	    	    	required: true,
	    	    	email: true,
	    	    	maxlength:100
	    	   	},
	    	   	password: {
	    	    	required: true,
	    	    	rangelength: [6,16]
	    	   	}
	    	},
	    	messages: {
	    	   	email: {
	    	    	required: "请输入登录邮箱地址",
	    	    	email: "请输入有效的邮箱地址，如：vivi@lagou.com",
	    	    	maxlength:"请输入100字以内的邮箱地址"
	    	   	},
	    	   	password: {
	    	    	required: "请输入密码",
	    	    	rangelength: "请输入6-16位密码，字母区分大小写"
	    	   	}
	    	},
	    	submitHandler:function(form){
	    		if($('#remember').prop("checked")){
	      			$('#remember').val(1);
	      		}else{
	      			$('#remember').val(null);
	      		}
	    		var email = $('#email').val();
	    		var password = $('#password').val();
	    		var remember = $('#remember').val();
	    		var rememberMe = (remember == undefined || remember == null) ? false : (remember == 1 ? true : false);
	    		
	    		/*var callback = $('#callback').val();
	    		var authType = $('#authType').val();
	    		var signature = $('#signature').val();
	    		var timestamp = $('#timestamp').val();*/
	    		
	    		$(form).find(":submit").attr("disabled", true);
	            $.ajax({
	            	type:'POST',
	            	data:{username:email, password:password, rememberMe:rememberMe},
	            	url:ctx+'/login/login.json',
	            	dataType: 'json'
	            }).done(function(result) {
					if(result.state == 1){
						var turl = "/grantServiceTicket/grant.html";
						window.location.href = turl;
					}else if(result.state == 240){
						$('#beError').text('请输入常用邮箱地址').show();
					}else if(result.state == 210){
						$('#beError').text('请输入100字以内的邮箱地址').show();
					}else if(result.state == 220){
						$('#beError').text('请输入有效的邮箱地址，如：vivi@lagou.com').show();
					}else if(result.state == 241){
						$('#beError').text('请输入密码').show();
					}else if(result.state == 211){
						$('#beError').text('请输入6-16位密码，字母区分大小写').show();
					}else if(result.state == 400){
						$('#beError').text('该帐号不存在或密码错误，请重新输入').show();
					}else if(result.state == 401){
						$('#beError').text('登录失败，该帐号已被禁用').show();
					}else if(result.state == 500){
						$('#beError').text('登录失败,系统内部异常').show();
					}else{
						$('#beError').text('网络异常，请刷新重试').show();
					}
					$(form).find(":submit").attr("disabled", false);
	            }); 
	        }  
		});
	
	 	var PARAMTER_VALUE = null;   
	 	function getParamter(paramName) {   
	 	    if(!PARAMTER_VALUE) {   //第一次初始化   
	 	        PARAMTER_VALUE = new Array();   
	 	        var paramStr = location.search.substring(1);   
	 	        var paramArr = paramStr.split("&");   
	 	        var len = paramArr.length;   
	 	        var tempArr;   
	 	        for(var i = 0; i < len; i++) {   
	 	            tempArr = paramArr[i].split("=");   
	 	            PARAMTER_VALUE[tempArr[0]] = tempArr[1];   
	 	        }   
	 	    }   
	 	    var paramValue = PARAMTER_VALUE[paramName];   
	 	    if(paramValue) {   
	 	        return paramValue;   
	 	    }   
	 	}  
})


//register
$(document).ready(function(e) {
    $('.register_radio li input').click(function(e){
		$(this).parent('li').addClass('current').append('<em></em>').siblings().removeClass('current').find('em').remove();
	});
	
	$('#email').focus(function(){
		$('#beError').hide();
	});
	//验证表单
	$("#registerForm").validate({
	        rules: {
	        	type:{
	        		required: true
	        	},
	    	   	email: {
	    	    	required: true,
	    	    	email: true,
	    	    	maxlength:100
	    	   	},
	    	   	password: {
	    	    	required: true,
	    	    	rangelength: [6,16]
	    	   	},
	    	   	checkbox:{required:true}
	    	},
	    	messages: {
	    		type:{
	        		required:"请选择使用拉勾的目的"
	        	},
	    	 	email: {
	    	    	required: "请输入常用邮箱地址",
	    	    	email: "请输入有效的邮箱地址，如：vivi@lagou.com",
	    	    	maxlength:"请输入100字以内的邮箱地址"
	    	   	},
	    	   	password: {
	    	    	required: "请输入密码", 
	    	    	rangelength: "请输入6-16位密码，字母区分大小写"
	    	   	},
	    	   	checkbox: {
	    	    	required: "请接受拉勾用户协议"
	    	   	}
	    	},
	    	errorPlacement:function(label, element){	    		
	    		/*modify nancy*/
	    		if(element.attr("type") == "radio"){
	    			label.insertAfter($(element).parents('ul')).css('marginTop','-20px');
	    		}else if(element.attr("type") == "checkbox"){
	    			label.insertAfter($(element).parent()).css('clear','left');
	    		}else{
	    			label.insertAfter(element);
	    		};	
	    	},
	    	submitHandler:function(form){
	    		var type =$('input[type="radio"]:checked',form).val();
	    		var email = $.trim($('#email').val());
	    		var password =$('#password').val();
	    		
	    		$(form).find(":submit").attr("disabled", true);

	            $.ajax({
	            	type:'POST',
	            	data: {email:email,password:password,type:type},
	            	url:ctx+'/register/register.json',
	            	dataType:'json'
	            }).done(function(result) {
	            	
	            	console.log(result);
	            	
	            	if(result.state == 1){
						var turl = "/grantServiceTicket/grant.html";
						window.location.href = turl;
						return false;
					}else if(result.state == 240){
						$('#beError').text('请输入常用邮箱地址').show();
					}else if(result.state == 210){
						$('#beError').text('请输入100字以内的邮箱地址').show();
					}else if(result.state == 220){
						$('#beError').text('请输入有效的邮箱地址，如：vivi@lagou.com').show();
					}else if(result.state == 241){
						$('#beError').text('请输入密码').show();
					}else if(result.state == 211){
						$('#beError').text('请输入6-16位密码，字母区分大小写').show();
					}else if(result.state == 243){
						$('#beError').text('请选择使用拉勾的目的').show();
					}else if(result.state == 400){
						$('#beError').text('该邮箱已被注册，请重新输入或直接登录').show();
					}else if(result.state == 500 || result.state == 501 || result.state == 502){
						$('#beError').text('注册失败,系统内部异常').show();
					}else{
						$('#beError').text('网络异常，请刷新重试').show();
					}
	            	
	            	$(form).find(":submit").attr("disabled", false);			           		
	            });
	        }  
	});
});